import React, { Component } from "react";
import TopNavBar from "@/components/TopNavBar";
import { MyDetail } from "./style";
import { connect } from "react-redux";
import * as actions from "@/store/Detail/actions";
class Detail extends Component {
  state= {
    done:false
  }
  componentDidMount() {
    this.props.getDetailAction();
  }

  saveHandle=(done)=>{ 
    console.log(this.props.detail);
    console.log(done);
    if(!done){
      sessionStorage.setItem("detail",JSON.stringify(this.props.detail))
    }else{
      sessionStorage.removeItem("detail");
    }
    this.setState({done:!done})
  }
  render() {
    const {done} = this.state
    return (
      <div>
        <TopNavBar {...this.props}>{this.props.detail.name}</TopNavBar>
        <MyDetail>
          <dt>
            <img
              src={this.props.detail.share_img}
              alt=""
            />
          </dt>
          <dd>
            <h3>{this.props.detail.name}</h3>
            <p>
              {this.props.detail.all_click}浏览 &nbsp;
              {this.props.detail.favorites}收藏
            </p>
          </dd>
          <button onClick={()=>this.saveHandle(done)}>
            {done ? "取消收藏" : "点击收藏"}
          </button>
          <br />
          <br />
          <div
            className="content"
            dangerouslySetInnerHTML={{ __html: this.props.detail.content }}
          ></div>
          <div className="footer">
            <p>小贴士</p>
            <h3>{this.props.detail.heaith_str}</h3>
            <h3>{this.props.detail.info}</h3>
          </div>
        </MyDetail>
      </div>
    );
  }
}

export default connect((state) => state.detail.toJS(), actions)(Detail);
